package com.geek.javase.ch08.test3;

/*
 *@author wangpeng
 *2025/10/16 17:33
 *@description TODO
 */
//BalanceNotEnoughException自定义异常
/*一个简易的银行取款操作，定义一个类ExceptionTest，
其中有String id 和double的balance两个属性，以及withdraw()取款方法。
通过withdraw()中的方法判断取出的钱，是否小于等于银行的余额。
通过main方法调用 withdraw()类 ，传入取出的钱，
并且用try-catch捕获异常并且处理，并且在finally块，显示余额。
需要的关键字（try-catch-finally throw throws）*/
public class ExceptionTest {
    public static void main(String[] args) throws Exception{
        Account acc = new Account("1",99999);
        System.out.println("取款前：" + acc);
        try{
            acc.withdraw(200000);
        }catch(BalanceNotEnoughException e){
            System.out.println("余额不足异常！");
        }finally {
            System.out.println();
        }
        System.out.println("取款后：" + acc);
    }
}
class  Account{
    //属性
    private String id;
    private double balance;

    public Account(){
    }
    public Account(String id, double balance){
        this.id = id;
        this.balance = balance;
    }
    //取款方法
    public  Account withdraw(double money) throws BalanceNotEnoughException {
        if(this.balance >= money){
            this.balance -= money;
            return this;
        }/*else {
            System.out.println("您的余额不足，无法取款！");
        }*/
        throw new BalanceNotEnoughException("余额不足！");
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    public String toString() {
        return "Account{id = " + id  + ", balance = " + balance + "}";
    }
}